<template>
  <a-modal
    title="新建教师"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item
              label="教师工号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['teacherCode', {rules: [
                  { required: true, message: '请输入教师工号', trigger: 'blur' },
                  { type: 'string', min: 8, message: '教师工号不允许小于8位', trigger: 'blur' }]}]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item
              label="教师姓名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['teacherName', {rules: [{required: true, message: '请输入教师姓名', trigger: 'blur'}]}]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item
              label="手机号码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['teacherPhone',
                              {rules: [{required: true, message: '请输入手机号码', trigger: 'blur'},
                                       { validator: validatePhone, trigger: 'blur' }]}]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { addTeacher, validateTeacherCode } from '@/api/basic/teacher'

export default {
  props: {
    roles: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,

      form: this.$form.createForm(this)
    }
  },
  methods: {
    add () {
      this.visible = true
    },
    // 前端验证手机号码
    validatePhone (rule, value, callback) {
      const reg = /^[1][3-9][0-9]{9}$/
      if (value === '' || value === undefined || value === null) {
        callback()
      } else {
        if ((!reg.test(value)) && value !== '') {
          callback(new Error('请输入正确的电话号码'))
        } else {
          callback()
        }
      }
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          validateTeacherCode({ 'teacherCode': values.teacherCode })
            .then(() => {
              values.roleId = 2
              addTeacher(values).then(() => {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              }).catch(err => {
                this.confirmLoading = false
                this.$message.error(err.result || '操作失败！')
              })
            }).catch(err => {
            this.confirmLoading = false
            this.$message.error(err.result || '教师工号已经存在！')
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
